package com.juc;

import java.util.concurrent.*;

public class PoolJuc {

    public static void main(String[] args) {
        // 固定大小线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);

        // 创建单个线程池
        ExecutorService pool2 = Executors.newSingleThreadExecutor();

        // 动态创建 来一个创建一个
        ExecutorService pool3 = Executors.newCachedThreadPool();


        // execute 子线程抛异常
        pool.execute(()-> {
            // 执行异步方法
        });
        // submit 异常只会在get抛出
        Future<?> ft1 =  pool.submit(()-> {
            // 执行
        });
        try {
            // 异常只能在get后报错
            ft1.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }


        // 关闭线程池 等任务执行完毕
        pool.shutdown();
        // 关闭线程池 只等待当前线程执行完成 排队的不管
        pool.shutdownNow();
        // 判断是否执行完成
        pool.isTerminated();
        // 等待线程池关闭
        try {
            pool.awaitTermination(2000, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
